﻿using System.Collections.Generic;
using EngineAdmin;
using DaphneControls.ViewModel;
using GalaSoft.MvvmLight;
using GameEngine;
using DaphneProfiBase;
using Tomers.WPF.Localization;
using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using System.Linq;
using System.Collections.Generic;

namespace DaphneForms.ViewModel
{
    /// <summary>
    /// Engine Match Form View Model
    /// </summary>
    public class EngineMatchVM : ViewModelBase
    {
        private enumGameType _checkersType = enumGameType.Czech;
        private int _numRounds = 1;
        private EngineConfig _whiteEngine;
        private EngineConfig _blackEngine;
        private string _sConfigKey = "EngineMatchConfig";

        /// <summary>
        /// Initializes a new instance of the EngineMatchVM class.
        /// </summary>
        public EngineMatchVM()
        {
            // try to load configuration from the global config
            RunMatchSettings settings = Config.Get<RunMatchSettings>(_sConfigKey);
            if (settings != null)
            {
                _checkersType = settings.CheckersType;
                _numRounds = settings.Rounds;
                _whiteEngine = settings.WhiteEngine;
                _blackEngine = settings.BlackEngine;
            }
            else
            {
                _whiteEngine = new EngineConfig();
                _blackEngine = new EngineConfig();
            }

            // init engine configuration
            WhiteConfig = new EngineConfigurationVM(_whiteEngine);
            BlackConfig = new EngineConfigurationVM(_blackEngine);

            // init commands
            RunMatchCommand = new RelayCommand(() =>
                {
                    RunMatch();
                });

            CancelCommand = new RelayCommand(() =>
                {
                    CancelForm();
                });


            // initialize data
            InitializeData();
        }

        #region Binding Properties

        public EngineConfigurationVM WhiteConfig
        { get; set; }

        public EngineConfigurationVM BlackConfig
        { get; set; }

        public List<CheckersType> CheckersTypes
        { get; set; }

        public CheckersType SelectedCheckers
        { get; set; }

        public string NumRounds
        { 
            get
            {
                return _numRounds.ToString();                
            }
            set
            {
                int.TryParse(value, out _numRounds);
            }
        }

        public RelayCommand RunMatchCommand
        { get; set; }

        public RelayCommand CancelCommand
        { get; set; }

        #endregion

        public bool DialogResult
        { get; set; }

        public RunMatchSettings Settings
        { get; set; }

        
        /// <summary>
        /// Initialize data for data-binding
        /// </summary>
        private void InitializeData() 
        {
            // checkers types
            CheckersTypes = new List<CheckersType>();

            foreach (var type in SupportedCheckers.Types)
            {
                CheckersType cType = new CheckersType();
                cType.GameType = type.CheckersType;
                cType.Title = LanguageContext.Instance.Dictionary.Translate<string>(type.CheckersNameKey, "Value");
                CheckersTypes.Add(cType);
            }

            SelectedCheckers = CheckersTypes.FirstOrDefault(i => i.GameType == _checkersType);
        }

        /// <summary>
        /// Runs the match
        /// </summary>
        private void RunMatch()
        {
            DialogResult = true;
            
            // create container class for match settings
            Settings = new RunMatchSettings();
            Settings.CheckersType = (SelectedCheckers as CheckersType).GameType;
            Settings.Rounds = _numRounds;
            Settings.WhiteEngine = _whiteEngine;
            Settings.BlackEngine = _blackEngine;

            // save the settings
            Config.Set<RunMatchSettings>(_sConfigKey, Settings);

            // exit form
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(Notify.ExitRunMatchForm));
        }

        /// <summary>
        /// Cancels the form
        /// </summary>
        private void CancelForm()
        {
            DialogResult = false;
            // exit form
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(Notify.ExitRunMatchForm));
        }


        public class CheckersType
        {
            public enumGameType GameType
            { get; set; }

            public string Title
            { get; set; }

            public override string ToString()
            {
                return Title;
            }
        }

    }
}